﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool;

namespace AutoMachine
{
    public partial class Frm_Login : Form
    {

       

        #region 窗体实例化
        /// <summary>
        /// 窗体对象实例化
        /// </summary>
        private static Frm_Login _instance;
        internal static Frm_Login Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Frm_Login();
                return _instance;
            }
        }
        #endregion

        //定义一个登录等级变化委托函数
        public delegate void LoginChangedHandler();

        //定义一个登录等级变化事件
        public static event LoginChangedHandler LoginChangedEvent;



        public Frm_Login()
        {
            InitializeComponent();         
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.cboUserName.SelectedIndex = 0;       
        }

        //登录按钮
        private void btnLogin_Click(object sender, EventArgs e)
        {
         
            if (txtPassWord.Text == string.Empty)
            {
                MessageBox.Show("请输入密码");
            }
            else if (this.cboUserName.Text == "管理员" && this.txtPassWord.Text == Tool.IniFile.INIGetStringValue(SysConfig.ConfigPath, "Config", "管理员", ""))
            {
                Device.ChangeOperationLevel(Device.OperationLevel.Admin);
                txtPassWord.Text = "";
                this.Close();
            }
           else if (this.cboUserName.Text == "开发人员" && this.txtPassWord.Text == Tool.IniFile.INIGetStringValue(SysConfig.ConfigPath, "Config", "开发人员", ""))
            {
                Device.ChangeOperationLevel(Device.OperationLevel.Developer);
                txtPassWord.Text = "";
                this.Close();
            }
           else if (this.cboUserName.Text == "操作员" && this.txtPassWord.Text == Tool.IniFile.INIGetStringValue(SysConfig.ConfigPath, "Config", "操作员", ""))
            {
                Device.ChangeOperationLevel(Device.OperationLevel.Operator);
                txtPassWord.Text = "";
                this.Close();
            }
            else 
            {
                txtPassWord.Text = "";
                //txtPassWord.Focus();
            }
            if (LoginChangedEvent != null)
            {
                LoginChangedEvent();
            }
        }
        
        private void txtPassWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(null, null);//等同于按下了登录键
            }
        }

        private void btnEixt_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //注销按钮
        public void btnLogout_Click(object sender, EventArgs e)
        {

            Device.ChangeOperationLevel(Device.OperationLevel.NoLevel);
            this.Close();
            if (LoginChangedEvent != null)
            {
                LoginChangedEvent();
            }
        }
    }
}
